package com.app.couponsharelife.ui.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.RemoteDataSource

/**
 * Created by guluwa on 2018/12/2.
 */
class LoginViewModel : BaseViewModel() {

    private var passwordLoginFresh = MutableLiveData<FreshBean>()

    private var passwordLoginResult: LiveData<ViewDataBean<ResultBean<TokenBean>>>? = null

    fun passwordLogin(): LiveData<ViewDataBean<ResultBean<TokenBean>>>? {
        if (passwordLoginResult == null) {
            passwordLoginResult = Transformations.switchMap(passwordLoginFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().passwordLogin(it.map)
                } else {
                    null
                }
            }
        }
        return passwordLoginResult!!
    }

    fun freshPasswordLogin(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) passwordLoginFresh.value = FreshBean(map, isFresh)
    }

    private var checkCodeLoginFresh = MutableLiveData<FreshBean>()

    private var checkCodeLoginResult: LiveData<ViewDataBean<ResultBean<TokenBean>>>? = null

    fun checkCodeLogin(): LiveData<ViewDataBean<ResultBean<TokenBean>>>? {
        if (checkCodeLoginResult == null) {
            checkCodeLoginResult = Transformations.switchMap(checkCodeLoginFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().checkCodeLogin(it.map)
                } else {
                    null
                }
            }
        }
        return checkCodeLoginResult!!
    }

    fun freshCheckCodeLogin(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) checkCodeLoginFresh.value = FreshBean(map, isFresh)
    }

    private var checkCodeLoginSendFresh = MutableLiveData<FreshBean>()

    private var checkCodeLoginSendResult: LiveData<ViewDataBean<ResultBean<Any>>>? = null

    fun checkCodeLoginSend(): LiveData<ViewDataBean<ResultBean<Any>>>? {
        if (checkCodeLoginSendResult == null) {
            checkCodeLoginSendResult = Transformations.switchMap(checkCodeLoginSendFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().checkCodeLoginSend(it.map)
                } else {
                    null
                }
            }
        }
        return checkCodeLoginSendResult!!
    }

    fun freshCheckCodeLoginSend(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) checkCodeLoginSendFresh.value = FreshBean(map, isFresh)
    }

    private var userRegisterFresh = MutableLiveData<FreshBean>()

    private var userRegisterResult: LiveData<ViewDataBean<ResultBean<TokenBean>>>? = null

    fun userRegister(): LiveData<ViewDataBean<ResultBean<TokenBean>>>? {
        if (userRegisterResult == null) {
            userRegisterResult = Transformations.switchMap(userRegisterFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().userRegister(it.map)
                } else {
                    null
                }
            }
        }
        return userRegisterResult!!
    }

    fun freshUserRegister(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) userRegisterFresh.value = FreshBean(map, isFresh)
    }

    private var userRegisterCheckCodeFresh = MutableLiveData<FreshBean>()

    private var userRegisterCheckCodeResult: LiveData<ViewDataBean<ResultBean<Any>>>? = null

    fun userRegisterCheckCode(): LiveData<ViewDataBean<ResultBean<Any>>>? {
        if (userRegisterCheckCodeResult == null) {
            userRegisterCheckCodeResult = Transformations.switchMap(userRegisterCheckCodeFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().userRegisterCheckCode(it.map)
                } else {
                    null
                }
            }
        }
        return userRegisterCheckCodeResult!!
    }

    fun freshUserRegisterCheckCode(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) userRegisterCheckCodeFresh.value = FreshBean(map, isFresh)
    }

    private var registerInformFresh = MutableLiveData<FreshBean>()

    private var registerInformResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun registerInform(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (registerInformResult == null) {
            registerInformResult = Transformations.switchMap(registerInformFresh) {
                if (it.isFresh) {
                    Log.e("eee","---->model回调了")
                    RemoteDataSource.getInstance().registerInform(it.map)
                } else {
                    null
                }
            }
        }
        return registerInformResult!!
    }

    fun freshRegisterInform(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) registerInformFresh.value = FreshBean(map, isFresh)
    }
    private var wxLoginFresh = MutableLiveData<FreshBean>()

    private var wxLoginResult: LiveData<ViewDataBean<RemoteResultBean<NewBindPhoneBean>>>? = null

    fun wxLogin(): LiveData<ViewDataBean<RemoteResultBean<NewBindPhoneBean>>>? {
        if (wxLoginResult == null) {
            wxLoginResult = Transformations.switchMap(wxLoginFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().wxLogin(it.map)
                } else {
                    null
                }
            }
        }
        return wxLoginResult!!
    }

    fun freshWxLogin(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) wxLoginFresh.value = FreshBean(map, isFresh)
    }

    private var bindPhoneFresh = MutableLiveData<FreshBean>()

    private var bindPhoneResult: LiveData<ViewDataBean<RemoteResultBean<NewBindPhoneBean>>>? = null

    fun bindPhone(): LiveData<ViewDataBean<RemoteResultBean<NewBindPhoneBean>>>? {
        if (bindPhoneResult == null) {
            bindPhoneResult = Transformations.switchMap(bindPhoneFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().bindPhone(it.map)
                } else {
                    null
                }
            }
        }
        return bindPhoneResult!!
    }

    fun freshBindPhone(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) bindPhoneFresh.value = FreshBean(map, isFresh)
    }
    private var wxRegisterFresh = MutableLiveData<FreshBean>()

    private var wxRegisterResult: LiveData<ViewDataBean<RemoteResultBean<NewBindPhoneBean>>>? = null

    fun wxRegister(): LiveData<ViewDataBean<RemoteResultBean<NewBindPhoneBean>>>? {
        if (wxRegisterResult == null) {
            wxRegisterResult = Transformations.switchMap(wxRegisterFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().wxRegister(it.map)
                } else {
                    null
                }
            }
        }
        return wxRegisterResult!!
    }

    fun freshWxRegister(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) wxRegisterFresh.value = FreshBean(map, isFresh)
    }
}